package com.ruoyi.web.controller.inspection;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.vo.InspectionPlanTaskVO;
import com.ruoyi.system.entity.*;
import com.ruoyi.system.mapper.InspectionPlanTaskDetailsMapper;
import com.ruoyi.system.mapper.PreventionHiddenInfoRecordMapper;
import com.ruoyi.system.mapper.PreventionHiddenRegisterMapper;
import com.ruoyi.system.service.InspectionPlanService;
import com.ruoyi.system.service.InspectionPlanTaskService;
import com.ruoyi.system.service.PreventionHiddenInfoRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 计划任务表(InspectionPlanTask)表控制层
 * @since 2022-11-28 17:55:24
 */
@RestController
@RequestMapping("/inspectionPlanTask")
public class InspectionPlanTaskController extends BaseController {

    @Resource
    private InspectionPlanService inspectionPlanService;
    @Resource
    private InspectionPlanTaskService inspectionPlanTaskService;
    @Resource
    private PreventionHiddenInfoRecordService preventionHiddenInfoRecordService;
    @Autowired
    private PreventionHiddenInfoRecordMapper preventionHiddenInfoRecordMapper;
    @Autowired
    private InspectionPlanTaskDetailsMapper inspectionPlanTaskDetailsMapper;
    @Autowired
    private PreventionHiddenRegisterMapper preventionHiddenRegisterMapper;

    /**
     * 分页查询
     * @param inspectionPlanTask 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(InspectionPlanTask inspectionPlanTask) {
        startPage();
        List<InspectionPlanTask> inspectionPlanTasks = inspectionPlanTaskService.queryByPage(inspectionPlanTask);
        if (inspectionPlanTasks.size()>0 && null != inspectionPlanTasks){
            for (InspectionPlanTask planTask : inspectionPlanTasks) {
                String countHidden = this.preventionHiddenInfoRecordService.countHidden(planTask.getId());
                planTask.setCountHiddenInfo(countHidden);
                List<InspectionPlanTaskVO> task= inspectionPlanTaskService.getCountAll(planTask.getId());
                if (task.size()>0 && null !=task){
                    for (InspectionPlanTaskVO inspectionPlanTaskVO : task) {
                        if (null !=inspectionPlanTaskVO ){
                            if (inspectionPlanTaskVO.getName().contains("总点位数")) {
                                planTask.setSumCount(inspectionPlanTaskVO.getDemp());
                            }else if (inspectionPlanTaskVO.getName().contains("剩余点位数")) {
                                planTask.setSurplusCount(inspectionPlanTaskVO.getDemp());
                            }
                        }
                    }
                }
            }
        }
        return getDataTable(inspectionPlanTasks);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.inspectionPlanTaskService.queryById(id));
    }

    /**
     * 新增数据
     * @param inspectionPlanTask 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(InspectionPlanTask inspectionPlanTask) {
        return AjaxResult.success(this.inspectionPlanTaskService.insert(inspectionPlanTask));
    }

    /**
     * 编辑数据
     * @param inspectionPlanTask 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(InspectionPlanTask inspectionPlanTask) {
        return AjaxResult.success(this.inspectionPlanTaskService.update(inspectionPlanTask));
    }

    /**
     * 删除数据
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.inspectionPlanTaskService.deleteById(id));
    }



    /**
     * 巡检任务_隐患数值穿透
     * @return 单条数据
     */
    @GetMapping("/queryPlanTaskId")
    public TableDataInfo queryPlanTaskId(Integer taskId) {
        startPage();
        List<PreventionHiddenRegister> preventionHiddenRegisters = new ArrayList<>();
        List<PreventionHiddenInfoRecord> recordList = this.preventionHiddenInfoRecordMapper.queryPlanTaskId(taskId);
        if (!CollectionUtils.isEmpty(recordList)){
            for (PreventionHiddenInfoRecord hiddenInfoRecord : recordList) {
                Integer hiddenId = hiddenInfoRecord.getHiddenId();
                PreventionHiddenRegister preventionHiddenRegister = preventionHiddenRegisterMapper.queryById(hiddenId);
                if (!ObjectUtils.isEmpty(preventionHiddenRegister)){
                    preventionHiddenRegisters.add(preventionHiddenRegister);
                }
            }
        }
        return getDataTable(preventionHiddenRegisters);
    }



    /**
     * 巡检任务_隐患数值穿透_历史隐患
     * @return 单条数据
     */
    @GetMapping("/queryPointIdList")
    public TableDataInfo queryPointIdList(Integer pointId) {
        startPage();
        List<PreventionHiddenInfoRecord> recordList = preventionHiddenRegisterMapper.queryPointIdList(pointId);
        return getDataTable(recordList);
    }


    /*
    2.巡检任务：
    巡检任务数量
    巡检完成度
    任务超时数量
     */
    @GetMapping("/countSize")
    public AjaxResult countSize() {
        Map<String,Object> map = inspectionPlanTaskService.countSize();
        return AjaxResult.success(map);
    }


    /**
     * 发现隐患的巡检记录
     */
    @GetMapping("/fxHiddenList")
    public TableDataInfo fxHiddenList(InspectionPlanTask inspectionPlanTask) {
        startPage();
        List<InspectionPlanTask> planTaskList = new ArrayList<>();
        List<InspectionPlanTask> inspectionPlanTasks = inspectionPlanTaskService.queryByPage(inspectionPlanTask);
        if (inspectionPlanTasks.size()>0 && null != inspectionPlanTasks){
            for (InspectionPlanTask planTask : inspectionPlanTasks) {
                List<InspectionPlanTaskVO> task= inspectionPlanTaskService.getCountAll(planTask.getId());
                if (task.size()>0 && null !=task){
                    for (InspectionPlanTaskVO inspectionPlanTaskVO : task) {
                        if (null !=inspectionPlanTaskVO ){
                            if (inspectionPlanTaskVO.getName().contains("总点位数")) {
                                planTask.setSumCount(inspectionPlanTaskVO.getDemp());
                            }else if (inspectionPlanTaskVO.getName().contains("剩余点位数")) {
                                planTask.setSurplusCount(inspectionPlanTaskVO.getDemp());
                            }
                        }
                    }
                }

                String countHidden = this.preventionHiddenInfoRecordService.countHidden(planTask.getId());
                if ("0".equals(countHidden)){
                    break;
                }
                planTask.setCountHiddenInfo(countHidden);
                InspectionPlan inspectionPlan = inspectionPlanService.queryById(planTask.getPlanId());
                planTask.setPlanName(inspectionPlan.getPlanName());
                planTaskList.add(planTask);
            }
        }
        return getDataTable(planTaskList);
    }

    /**
     * APP首页 我的巡检
     * @return
     */
    @GetMapping("/getCountApp")
    public AjaxResult getCountApp() {

        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        InspectionPlanTask inspectionPlanTask1 = new InspectionPlanTask();
        inspectionPlanTask1.setPlanUserNames(nickName);
        inspectionPlanTask1.setPlanTaskState("未完成");
        List<InspectionPlanTask> inspectionPlanTasks1 = inspectionPlanTaskService.queryByPage(inspectionPlanTask1);

        InspectionPlanTask inspectionPlanTask2 = new InspectionPlanTask();
        inspectionPlanTask2.setPlanUserNames(nickName);
        inspectionPlanTask2.setPlanTaskState("已完成");
        List<InspectionPlanTask> inspectionPlanTasks2 = inspectionPlanTaskService.queryByPage(inspectionPlanTask2);

        InspectionPlanTask inspectionPlanTask3 = new InspectionPlanTask();
        inspectionPlanTask3.setPlanUserNames(nickName);
        inspectionPlanTask3.setPlanTaskState("已超时");
        List<InspectionPlanTask> inspectionPlanTasks3 = inspectionPlanTaskService.queryByPage(inspectionPlanTask3);

        Map<String,Integer> map = new HashMap<>();
        map.put("wan", CollUtil.isEmpty(inspectionPlanTasks2) ? 0 : inspectionPlanTasks2.size());
        map.put("chao", CollUtil.isEmpty(inspectionPlanTasks3) ? 0 : inspectionPlanTasks3.size());
        map.put("dai", CollUtil.isEmpty(inspectionPlanTasks1) ? 0 : inspectionPlanTasks1.size());

        return AjaxResult.success(map);
    }

}

